#!/bin/bash

usage() {
cat <<EOF

usage:
    --image-name gs-tool
    --version v0.1.0
    --image-repo xisheng
    -h print help

    example: sh build.sh --image-name crm --version v0.1.0 --image-repo xisheng

EOF
}
BIN_FILE="gs-tool"

#echo "set build info"
#GIT_COMMIT=$(git rev-parse --short HEAD || echo "GitNotFound")
#BUILD_TIME=$(date +%FT%T%z)

#echo $GIT_COMMIT $BUILD_TIME
#LDFLAGS="-X main.GitCommit=${GIT_COMMIT} -X main.BuildTime=${BUILD_TIME}"

buildProgram(){
  CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/linux/${BIN_FILE} ./cmd/changelogo.go

  if [ $? -ne 0 ]; then
    echo "compile failed"
    exit 1
  fi
  echo "***** compile success *****"
}

set -e

main() {
  buildProgram
}

if [ "$1" = "-h" ]; then
  usage
  exit 0
fi

while [ $# -gt 0 ]
do
    key="$1"
    case $key in
        --image-name)
            export IMAGE_NAME=$2
            shift
        ;;
        --version)
            export VERSION=$2
            shift
        ;;
        --image-repo)
            export IMAGE_REPO=$2
            shift
        ;;
        *)
            echo "unknown option [$key]"
            usage
            exit 1
        ;;
    esac
    shift
done

if [ -z "$IMAGE_NAME" ]; then
  IMAGE_NAME='gs-tool'
fi

if [ -z "$VERSION" ]; then
  VERSION='v0.1.1'
fi

if [ -z "$IMAGE_REPO" ]; then
  IMAGE_REPO='xisheng'
fi

main